package common.design.observer;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by dong peng on 2018/1/28.
 *
 * 主题的实现类之一
 */
public class TopicAImpl implements Topic {

    private String data;
    private List<Observer> observerList = new ArrayList<Observer>();

    public void updateData(String data) {
        this.data = data;
        System.out.println("主题"+TopicAImpl.class.getName()+"的数据内容变更为："+data);
        this.notifyObservers();
    }

    public void notifyObservers() {

        // 通知所有订阅者
        for(Observer observer : observerList) {
            observer.onMessageEvent(this);
        }
    }

    public void subscribe(Observer observer) {
        observerList.add(observer);
    }

    public void removeSub(Observer observer) {
        observerList.remove(observer);
    }

    public String getData() {
        return data;
    }

    public void setData(String data) {
        this.data = data;
    }
}
